package cl.pds.pruebacliente;

import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.JsonObject;
public class UpdatePacienteActivity extends Activity{

	private String URL ="";
	private String NAMESPACE = "http://";

	Paciente paciente;
	EditText RUTEditText;
	EditText nombreEditText;
	EditText direccionEditText;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_updatepaciente);
		RUTEditText = (EditText) findViewById(R.id.edTxtUpdRUT);
		nombreEditText = (EditText) findViewById(R.id.addpacienteUpd_nombre);
		direccionEditText = (EditText) findViewById(R.id.edTxtUpdDireccion);
		//new ObtenerGenerosClasificacionBackground().execute();
	}

	public void guardar(View v){
		JsonObject jsonObjectPaciente = new JsonObject();
		
		jsonObjectPaciente.addProperty("idPaciente", paciente.getIdPaciente());
		jsonObjectPaciente.addProperty("RUT", paciente.getRUT().toString());
		jsonObjectPaciente.addProperty("Nombre", nombreEditText.getText().toString());
		jsonObjectPaciente.addProperty("Direccion", direccionEditText.getText().toString());
		
		Log.d("",jsonObjectPaciente.toString());
		new UpdatePeliculaBackground().execute(jsonObjectPaciente.toString());
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_updatepaciente, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.remove_paciente:
            	Log.d("",paciente.getIdPaciente());
            	new RemovePeliculaBackground().execute(Integer.parseInt(paciente.getIdPaciente()));
            	return true;
            default:
                return true;
        }
    }

	class UpdatePeliculaBackground extends AsyncTask<String, Void, String>{

		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(
					UpdatePacienteActivity.this,
					"Lista de pacientes", 
					"Espere mientras se actualizando el paciente...", 
					true);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {

			String resultado = new String();
			String METHOD_NAME = "updatePaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("jsonPelicula", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(UpdatePacienteActivity.this, "Ingresado correctamente", Toast.LENGTH_LONG).show();
				finish();
			}else{
				Toast.makeText(UpdatePacienteActivity.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
			}
		}
	}

	class RemovePeliculaBackground extends AsyncTask<Integer, Void, String>{

		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(
					UpdatePacienteActivity.this,
					"Cartelera de Cine", 
					"Espere mientras se elimina la pelicula...", 
					true);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Integer... params) {

			String resultado = new String();
			String METHOD_NAME = "removePaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("peliculaId", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(UpdatePacienteActivity.this, "Eliminado correctamente", Toast.LENGTH_LONG).show();
				finish();
			}else{
				Toast.makeText(UpdatePacienteActivity.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
			}
		}
	}


}
